export default {
    changeLanguage(state,lang){
        state.language = lang;
        localStorage.setItem('language',lang);
    },
    changeMenu(state,menu){
        state.menu = menu;
        localStorage.setItem('menu',menu);
    },

    setIsCollapsed(state,isCollapsed) {
        state.isCollapsed = isCollapsed
    },
    setStockId(state,stockId) {
        state.stockId = stockId
    },
    getPartList(state,partList){
        state.partList = partList
    },
    addPart(state,part){
        state.partList.unshift(part)
    },
    modPart(state,part){
        let partList = state.partList;
        let idx = partList.findIndex(item => item.id === part.id);
        partList.splice(idx,1,part)
    },
    modPartNum(state,part) {
        let partList = state.partList;
        if(!partList.length){
            return
        }
        let targetList = partList.filter(item => {
            return part.find(p=>p.partsId === item.id);
        });
        if(targetList) {
            for (let i=0;i<part.length;i++) {
                targetList[i].quantity = targetList[i].quantity - part[i].partsQuantity;
                targetList[i].totalPrice = targetList[i].quantity * targetList[i].salePrice;
            }
        }
    },
    modQuantity(state,part){
        const idx = state.partList.findIndex(item => item.id === part.id);
        if(idx) {
            state.partList.splice(idx,1,part)
        }
    },
    modPartPrice(state,part){
        let newVal = '';
        const idx = state.partList.findIndex(item => item.id === part.partsId);
        if (idx) {
            newVal = {...state.partList[idx]};
            newVal.purchasePrice = part.purchasePrice;
            newVal.salesPrice  = part.salesPrice;
            state.partList.splice(idx,1,newVal)
        }
    },
    getCustomerList(state,list){
        state.customerList = list;
    },
    getEmployeeList(state,list){
        state.employeeList = list
    },
    addEmployee(state,employee){
        state.employeeList.push(employee)
    },
    modEmployee(state,employee){
        const idx = state.employeeList.findIndex(item=>item.id===employee.id);
        state.employeeList.splice(idx,1,employee);
    },
    deleteEmployee(state,employee){
        const idx = state.employeeList.findIndex(item=>item.id===employee.id);
        state.employeeList.splice(idx,1);
    },
    setAuth(state,auth) {
        state.auth = auth
    },
    setUser(state,user) {
        state.user = user
    }
}